package com.m7788.dlg;

import com.m7788.screen.R;
import com.m7788.util.AppUtil;
import com.m7788.util.Constant;
import com.tool.utils.DataUtil;

import android.content.Context;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
import android.graphics.drawable.BitmapDrawable;
import android.support.v4.view.ViewPager.LayoutParams;
import android.text.TextUtils;
import android.view.Gravity;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ImageView;
import android.widget.PopupWindow;
import android.widget.TextView;

public class SystemSetPop extends PopupWindow implements OnClickListener {
	private Context mContext;
	private View rootView;
	private ImageView mIvVoiceSwitch, mIvShakeSwitch,mIvDelete;
	private TextView mTvClearStorage, mTvClearCookies, mTvVersion,mTvVoiceClose,mTvShakeClose;

	public SystemSetPop(Context mContext) {
		super(mContext);
		this.mContext = mContext;
		this.rootView = View.inflate(mContext, R.layout.layout_pop_system_set,
				null);
		setFocusable(true);
		setOutsideTouchable(true);
		setWidth(LayoutParams.MATCH_PARENT);
		setHeight(LayoutParams.MATCH_PARENT);
		setContentView(rootView);
		setBackgroundDrawable(new BitmapDrawable());
		findComponent();
		initComponent();
	}

	private void initComponent() {
		mIvVoiceSwitch.setOnClickListener(this);
		mIvShakeSwitch.setOnClickListener(this);
		mTvClearStorage.setOnClickListener(this);
		//mTvClearCookies.setOnClickListener(this);
		mIvDelete.setOnClickListener(this);
		mTvVersion.setText("版本信息   V"+getAppVersionName(mContext));
		String voice = AppUtil.getRecord(mContext, Constant.SOUND_KEY);
		String shake = AppUtil.getRecord(mContext, Constant.SHAKE_KEY);
		if(TextUtils.isEmpty(voice)){
			mIvVoiceSwitch.setSelected(true);
			mTvVoiceClose.setText("开启");
			AppUtil.saveRecord(mContext, Constant.SOUND_KEY, "open");
		}else{
			if(voice.equals("open")){
				mIvVoiceSwitch.setSelected(true);
				mTvVoiceClose.setText("开启");
			}else{
				mIvVoiceSwitch.setSelected(false);
				mTvVoiceClose.setText("关闭");
			}
		}
		if(TextUtils.isEmpty(shake)){
			mIvShakeSwitch.setSelected(false);
			mTvShakeClose.setText("关闭");
			AppUtil.saveRecord(mContext, Constant.SHAKE_KEY, "close");
		}else{
			if(shake.equals("open")){
				mIvShakeSwitch.setSelected(true);
				mTvShakeClose.setText("开启");
			}else{
				mIvShakeSwitch.setSelected(false);
				mTvShakeClose.setText("关闭");
			}
		}
	}

	private void findComponent() {
		mIvVoiceSwitch = (ImageView) rootView.findViewById(R.id.iv_voiceSwitch);
		mIvShakeSwitch = (ImageView) rootView.findViewById(R.id.iv_ShakeSwitch);
		mTvClearStorage = (TextView) rootView
				.findViewById(R.id.tv_clearStorage);
		//mTvClearCookies = (TextView) rootView.findViewById(R.id.tv_clearCookie);
		mTvVersion = (TextView) rootView.findViewById(R.id.tv_Version);
		mIvDelete = (ImageView) rootView.findViewById(R.id.iv_delete);
		mTvVoiceClose = (TextView) rootView.findViewById(R.id.tv_voiceClose);
		mTvShakeClose = (TextView) rootView.findViewById(R.id.tv_shakeClose);
	}
	
	private String getAppVersionName(Context context) {
		String versionName = "";
		try {
			PackageManager packageManager = context.getPackageManager();
			PackageInfo packageInfo = packageManager.getPackageInfo(
					"com.m7788.screen", 0);
			versionName = packageInfo.versionName;
			if (TextUtils.isEmpty(versionName)) {
				return "";
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		return versionName;
	}

	@Override
	public void onClick(View v) {
		int id = v.getId();
		if (id == R.id.iv_voiceSwitch) {
			if(mIvVoiceSwitch.isSelected()){
				AppUtil.saveRecord(mContext, Constant.SOUND_KEY, "close");
				mIvVoiceSwitch.setSelected(false);
				mTvVoiceClose.setText("关闭");
			}else{
				AppUtil.saveRecord(mContext, Constant.SOUND_KEY, "open");
				mIvVoiceSwitch.setSelected(true);
				mTvVoiceClose.setText("开启");
			}
		} else if (id == R.id.iv_ShakeSwitch) {
			if(mIvShakeSwitch.isSelected()){
				AppUtil.saveRecord(mContext, Constant.SHAKE_KEY, "close");
				mIvShakeSwitch.setSelected(false);
				mTvShakeClose.setText("关闭");
			}else{
				AppUtil.saveRecord(mContext, Constant.SHAKE_KEY, "open");
				mIvShakeSwitch.setSelected(true);
				mTvShakeClose.setText("开启");
			}
		} else if (id == R.id.tv_clearStorage) {
			long before = AppUtil.getAvailMemory(mContext);
			AppUtil.clearMemory(mContext);
			long after = AppUtil.getAvailMemory(mContext);
			DataUtil.showShortToast(mContext,
					"已释放内存" + Math.abs(after - before) + "M");
			/*} else if (id == R.id.tv_clearCookie) {
			CookieSyncManager.createInstance(mContext);
			CookieManager cookieManager = CookieManager.getInstance();
			DataUtil.showShortToast(mContext, "COOKIE清除成功!");
			cookieManager.removeAllCookie();*/
		}else if(id == R.id.iv_delete){
			dismiss();
		}
	}

	public void show(View view) {
		showAtLocation(view, Gravity.NO_GRAVITY, 0, 0);
	}

}
